package com.example.d2draw;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import static com.example.d2draw.R.*;
import static com.example.d2draw.R.color.purple_200;

public class CircleView extends View {

    private Context context;

    private int mCircleCoior;
    //如果仅在java中使用，仅重写该构造方法就行
    public CircleView(Context context) {
        super(context);
        this.context=context;
    }

    //如果要在java和xml同时使用，重写其构造方法
    public CircleView(Context context, @Nullable AttributeSet attrs) {
        //super(context, attrs);
        this(context,attrs, 0);

        this.context=context;
    }

    //若自定义控件需要支持style风格样式，则重写该构造方法
    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        TypedArray array=context.obtainStyledAttributes(attrs,styleable.CardView);
        mCircleCoior=array.getColor(styleable.CircleView_circle_color,Color.BLUE);
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //设定控件最小宽高
        int defaultWidth=240;
        int defauLtHeight=240;
        //获取测量宽高
        int width=MeasureSpec.getSize(widthMeasureSpec);
        int hight=MeasureSpec.getSize(heightMeasureSpec);

        if (getLayoutParams().width==ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height==ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(defaultWidth,defauLtHeight);
        }else if (getLayoutParams().width==ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(defaultWidth,hight);
        }else if (getLayoutParams().height==ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(width,defauLtHeight);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画笔
        Paint paint=new Paint();
        //画笔的颜色
        paint.setColor(mCircleCoior);
        //获取控件的宽高
//        int width=getWidth();
//        int hight =getHeight();
//        //半径
//        int r=Math.min(width,hight)/2;
//        //画圆(内切圆)
//        canvas.drawCircle(width/2,hight/2,r,paint);
        setBackgroundColor(context.getColor(purple_200));
//        canvas.drawLine(0,0,20,50,paint);
//        canvas.drawLine(20,50,40,30,paint);
//        canvas.drawLine(40,30,60,80,paint);
        //获取padding值
        final int paddingLeft = getPaddingLeft();
        final int paddingRight = getPaddingRight();
        final int paddingTop = getPaddingTop();
        final int paddingBottom = getPaddingBottom();
        int width =getWidth()-paddingLeft- paddingRight;
        int height= getHeight()- paddingTop- paddingBottom;
        //设置圆的半径=宽,高最小值的2分之1
        int r= Math. min(width, height)/2;
        //画出圆(蓝色)
        //圆心=控件的中央,半径=宽,高最小值的2分之1
        canvas.drawCircle(paddingLeft+width/2, paddingTop+height/2, r, paint);




    }


}
